package cn.tedu.ymg.product.controller;

import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.web.JsonResult;
import cn.tedu.ymg.product.pojo.param.ParticularsAddNewParam;
import cn.tedu.ymg.product.pojo.param.ParticularsUpdateInfoParam;
import cn.tedu.ymg.product.pojo.param.SubscribeAddNewParam;
import cn.tedu.ymg.product.pojo.param.SubscribeUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.ParticularsListItemVO;
import cn.tedu.ymg.product.pojo.vo.ParticularsStandardVO;
import cn.tedu.ymg.product.pojo.vo.SubscribeListItemVO;
import cn.tedu.ymg.product.pojo.vo.SubscribeStandardVO;
import cn.tedu.ymg.product.service.IParticularsService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/particulars")
@Api(tags = "09.酒店详情管理模块")
@Slf4j
@Validated
public class ParticularsController {

    @Autowired
    private IParticularsService service;

    @PostMapping("/add-new")
    @ApiOperation("添加酒店详情")
    @ApiOperationSupport(order = 100)//设置显示顺序
    //@Valid作用:以表示“将检查此参数的基本有效性”，就是检查参数是否符合要求是否正确
    public JsonResult addNew(@Valid ParticularsAddNewParam particularsAddNewParam) {
        log.debug("开始处理【添加酒店详情】的请求，参数：{}", particularsAddNewParam);
        service.addNew(particularsAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/{id}/delete")
    @ApiOperation("根据ID删除酒店详情")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "酒店详情ID", name = "id", required = true, dataType = "long"),
    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除酒店详情失败,请提交合法的ID值")
                             @PathVariable Long id) {
        log.debug("开始处理【删除酒店详情】的请求，参数：{}", id);
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改酒店详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "酒店详情ID", name = "id", required = true, dataType = "long"),
    })
    public JsonResult update(@Range(min = 1,message ="根据ID修改酒店详情失败,请提交合法的ID值")
                             @RequestParam Long id,
                             @Valid ParticularsUpdateInfoParam particularsUpdateInfoParam) {
        log.debug("开始处理【修改酒店详情】的请求，参数：{}", particularsUpdateInfoParam);
        service.updateInfoById(id, particularsUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("{id:[0-9]+}")
    @ApiOperation("根据ID查询酒店详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "酒店详情ID", name = "id", required = true, dataType = "long"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询酒店详情失败,请提交合法的ID值")
                                      @PathVariable Long id) {
        log.debug("开始处理【根据ID查询酒店详情】的请求，参数：{}", id);
        ParticularsStandardVO queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/list")
    @ApiOperation("查询酒店详情列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(@Range(min = 1, message = "查询酒店详情列表失败，请提供正确的页码值！") Integer page,
                           String queryType) {
        log.debug("开始处理【查询酒店详情列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<ParticularsListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = service.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
